using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Core;
using TMPro;

public class LoginPanel : BasePanel
{
    protected override void OnInit(object data)
    {
        Debug.Log("open LoginPanel");
        ShowTestServer();

        GetReference<ButtonEx>("battleBtn").onClick = () =>
        {
            new LoadScene(ScenePath.Battle).Publish();
        };
    }

    private ScrollList<int> serverList;
    private void ShowTestServer()
    {
        var list = new List<int>();
        for (int i = 0; i < 30; i++)
        {
            list.Add(i + 1);
        }

        var scrollEx = GetReference<ScrollRectEx>("serverScrollEx");
        serverList = scrollEx.Init<int>(OnRefreshServerItem);
        serverList.Datas = list;
    }

    private void OnRefreshServerItem(ref ScrollListData<int> listData)
    {
        var group = listData.item.GetComponent<ReferenceGroup>();
        group.GetReference<TextMeshProUGUI>("idTxt").text = $"ID:{listData.data}";

        var serverItemReference = group.GetReference<GameReference>("serverItem");
        LoadServerItem(serverItemReference, listData.data);
    }

    private async void LoadServerItem(GameReference reference, int id)
    {
        var serverItem = await reference.LoadAsync<ServerItem>();
        Debug.Log($"serverID:{id}");
        serverItem.SetData(id);
    }
}
